home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr46 / vfwdk.zip / VFWSDK.ZIP / SAMPLES / BRAVADO / MAPC.C < prev    next >
C/C++ Source or Header  |  1993-01-31  |  4KB  |  146 lines

  1. /****************************************************************************
  2.  *
  3.  *   mapc.c
  4.  * 
  5.  *   Routines to handle YUV to 8-bit palette translations
  6.  *
  7.  *   Microsoft Video for Windows Sample Capture Driver
  8.  *   Chips & Technologies 9001 based frame grabbers.
  9.  *
  10.  *   Copyright (c) 1992-1993 Microsoft Corporation.  All Rights Reserved.
  11.  *
  12.  *    You have a royalty-free right to use, modify, reproduce and 
  13.  *    distribute the Sample Files (and/or any modified version) in 
  14.  *    any way you find useful, provided that you agree that 
  15.  *    Microsoft has no warranty obligations or liability for any 
  16.  *    Sample Application Files which are modified. 
  17.  *
  18.  ***************************************************************************/
  19.  
  20. #include <windows.h>
  21. #include <mmsystem.h>
  22. #include <msvideo.h>
  23. #include <msviddrv.h>
  24. #include "ct.h"
  25.  
  26. #define NUM_GRAY 64
  27.  
  28. /* TransInit - Translate Init
  29.  *
  30.  * Initalize the translation table
  31.  *    Alloc the translate tables
  32.  * 
  33.  * Return 0 on success
  34.  *
  35.  */
  36. WORD FAR PASCAL TransInit( void )
  37. {
  38.     WORD w;
  39.     LPBYTE pb;
  40.  
  41.     /* First alloc the 32K table */
  42.     fpTrans16to8 = GlobalLock(GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE, 
  43.         1024L * 32L));
  44.  
  45.     if (fpTrans16to8 == NULL)
  46.         return 1;       // error no memory
  47.  
  48.     fpCopyBuffer = GlobalLock(GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE, 
  49.         (DWORD) gwWidth * (DWORD) gwHeight * 2));
  50.  
  51.     if (fpCopyBuffer == NULL)
  52.         return 1;       // error no memory
  53.  
  54.     fpYUVtoRGB16 = (LPWORD) GlobalLock(GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE, 
  55.         (DWORD) 64L * 1024L));
  56.  
  57.     if (fpYUVtoRGB16 == NULL)
  58.         return 1;       // error no memory
  59.  
  60.     /* set up init palette */
  61.     palCurrent.palVersion = 0x0300;
  62.     palCurrent.palNumEntries = NUM_GRAY;
  63.  
  64.     //
  65.     //  make a 64 gray scale palette as default.
  66.     //
  67.     for (w=0; w<NUM_GRAY; w++) {
  68.         palCurrent.palPalEntry[w].peRed   = (BYTE)(w * 255/(NUM_GRAY-1));
  69.         palCurrent.palPalEntry[w].peGreen = (BYTE)(w * 255/(NUM_GRAY-1));
  70.         palCurrent.palPalEntry[w].peBlue  = (BYTE)(w * 255/(NUM_GRAY-1));
  71.         palCurrent.palPalEntry[w].peFlags = 0;
  72.     }
  73.  
  74.     pb = fpTrans16to8;
  75.     for (w = 0; w < 0x8000; w++)
  76.        *pb++ = (BYTE)(WORD)((WORD) w / ((WORD) 0x8000 / NUM_GRAY));
  77.  
  78.     return 0;   // no error
  79. }
  80.  
  81.  
  82. /* TransFini - Translate Finalize
  83.  *
  84.  * Clean up the translation table
  85.  *
  86.  * 
  87.  */
  88. void FAR PASCAL TransFini( void )
  89. {
  90.     if (fpTrans16to8) {
  91.         GlobalFree(HIWORD(fpTrans16to8));
  92.         fpTrans16to8 = NULL;
  93.     }
  94.     if (fpCopyBuffer) {
  95.         GlobalFree(HIWORD(fpCopyBuffer));
  96.         fpCopyBuffer = NULL;
  97.     }
  98.     if (fpYUVtoRGB16) {
  99.         GlobalFree(HIWORD(fpYUVtoRGB16));
  100.         fpYUVtoRGB16 = NULL;
  101.     }
  102. }
  103.  
  104.  
  105. /* TransCalcNew - Calculate a new YUV translation table
  106.  *
  107.  * R = V * 179/127 + Y
  108.  * B = U * 226/127 + Y
  109.  * G = 1.706*Y + .509*R +.194*B
  110.  */
  111. BOOL FAR PASCAL TransRecalcPal( HPALETTE hpal )
  112. {
  113.     int r,g,b;
  114.     int k;
  115.     int y,iu,iv;
  116.     int ScaledU, ScaledV;
  117.     LPSTR pb;
  118.     char u,v;
  119.  
  120.     pb = fpTrans16to8;
  121.  
  122.     for (k=0; k<256; k+=8) {
  123.         y = k;
  124.     for (iu=0; iu<256; iu+=8) {
  125.         for (iv=0; iv<256; iv+=8) {
  126.                 u = (char)(BYTE)iu;
  127.                 v = (char)(BYTE)iv;
  128.                 ScaledU = (int)((u*226L)/127); 
  129.                 ScaledV = (int)((v*179L)/127);
  130.                 r = max(0, min(255, (int) (ScaledV + (int)y)));
  131.                 b = max(0, min(255, (int) (ScaledU + (int)y)));
  132.             g = max(0, min(255, (int) ((y * 1706L)/1000 - ((r * 509L)/1000) - ((b * 194L)/1000))));
  133.  
  134.         *pb++ = (BYTE)GetNearestPaletteIndex(hpal, RGB(r,g,b) );
  135.         }
  136.     }
  137.     }
  138.  
  139.     palCurrent.palVersion = 0x0300;
  140.     GetObject(hpal, sizeof(int), (LPVOID)&palCurrent.palNumEntries);
  141.     GetPaletteEntries( hpal, 0, palCurrent.palNumEntries, palCurrent.palPalEntry);
  142.         
  143.     return TRUE;
  144. }
  145.  
  146.